package com.seelyn.ddvideo;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.Calendar;
import java.util.List;

/**
 * @author slacrey
 * @since 2017/6/11
 */
public class QQVideoInfoTestProcessor implements PageProcessor {

    private String imgXpathString = "/html/body/div[@class*='warp']/div[1]/div/div/div[@class*='vodImg']/img";

    private Site site = Site.me().setDomain("http://ziyuanpian.com")
            .setCharset("UTF-8")
            .setSleepTime(100)
            .setTimeOut(5000)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");


    @Override
    public void process(Page page) {

        Html body = page.getHtml();
        Selectable spanItems = body.xpath(imgXpathString);

        if (spanItems == null || spanItems.all().isEmpty()) {
            return;
        }
        Html imgHtml = Html.create(spanItems.get());
        Element imgElement = imgHtml.getDocument().body().child(0);
        String picUrl = imgElement.attr("src");
        if (StringUtils.isEmpty(picUrl)) {
            return;
        }

        if (picUrl.startsWith("http://")) {
            picUrl = site.getDomain() + picUrl;
        }

        Calendar calendar = Calendar.getInstance();
        String targetName = picUrl.substring(picUrl.lastIndexOf("/") + 1);
        String targetFile = calendar.get(Calendar.YEAR) + "/" + calendar.get(Calendar.MONTH) + "/" + targetName;

        Boolean link = false;
        Html inputHtml = null;
        Html playInfoHtml = null;
        String html22 = null;
        List<Selectable> selectables = body.$(".vodplayinfo").nodes();
        for (Selectable selectable : selectables) {
            html22 = selectable.get();
            if (html22.contains("播放类型：zuidam3u8")) {
                playInfoHtml = Html.create(html22);
                playInfoHtml.getDocument().body();

                link = documentHandler(playInfoHtml.getDocument().body(), "播放类型：zuidam3u8", "直接复制链接", false);
                if (StringUtils.isEmpty(link)) {
                    continue;
                }

                break;
            }
        }

    }

    private Boolean documentHandler(Element rootElement, String startWithStr, String endWithStr, boolean matchFlag) {
        Boolean exist = false;
        Elements listElement = rootElement.children();
        for (Element element : listElement) {
            if (!matchFlag && startWithStr.equals(element.ownText())) {
                matchFlag = true;
            }

            if (matchFlag) {

                Elements inputList = element.getElementsByTag("li");
                if (!inputList.isEmpty()) {
                    int order = 0;
                    for (Element element1 : inputList) {
                        String text = element1.text();
                        String[] textArray = text.split("\\$");
                        String name = textArray[0];
                        String link = textArray[1];
                        order++;
                        System.out.println(text);
                    }
                    exist = true;
                    continue;
                }

                String val = element.attr("value");
                if (!StringUtils.isEmpty(val) && endWithStr.equals(val)) {
                    break;
                }

            } else {
                exist = documentHandler(element, startWithStr, endWithStr, matchFlag);
                if (exist) {
                    break;
                }
            }
        }
        return exist;
    }

    @Override
    public Site getSite() {
        return site;
    }

}
